package com.hanyc.spring.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanyc.spring.result.Result;

import com.hanyc.spring.dto.Demo1DTO;
import com.hanyc.spring.vo.Demo1VO;
import com.hanyc.spring.entity.Demo1;
import com.hanyc.spring.util.QueryUtils;
import com.hanyc.spring.service.IDemo1Service;
import com.hanyc.spring.util.MapperUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;



import java.util.Arrays;
import java.util.List;


/**
 * 【DEMO_1】控制器层
 *
 * @author hanyc
 * @date 2025-06-13
 */
@Slf4j
@Api(tags = {"【DEMO_1】模块Controller"})
@RestController
@RequiredArgsConstructor
@RequestMapping("/demo1")
public class Demo1Controller {

    private final IDemo1Service  demo1Service;

    @ApiOperation(value = "DEMO_1列表查询")
    @PostMapping("/list")
    public Result<List<Demo1VO>> list(@RequestBody Demo1DTO dto) {
        return Result.ok(MapperUtils.INSTANCE.mapAsList(Demo1VO.class,demo1Service.list(Wrappers.lambdaQuery(BeanUtil.copyProperties(dto, Demo1.class)))));
    }

    @ApiOperation(value = "DEMO_1分页查询")
    @PostMapping("/page")
    public Result<Page<Demo1VO>> page(@RequestBody Demo1DTO dto) {
        Page<Demo1> page = new Page<>(dto.getCurrent(), dto.getSize());
        Page<Demo1> result = demo1Service.page(page, QueryUtils.dynamicSort(dto.getSortBy(), Wrappers.query(BeanUtil.copyProperties(dto, Demo1.class))));
        return Result.ok(MapperUtils.INSTANCE.mapAsPage(Demo1VO.class, result));
    }

    @ApiOperation(value = "DEMO_1添加,并获取ID")
    @PostMapping("/insertReturnId")
    public Result<Long> insertReturnId(@RequestBody Demo1DTO dto) {
        Demo1 entity = BeanUtil.copyProperties(dto, Demo1.class);
        demo1Service.save(entity);
        return Result.ok(entity.getId());
    }

    @ApiOperation(value = "DEMO_1添加")
    @PostMapping
    public Result<Boolean> insert(@RequestBody Demo1DTO dto) {
        Demo1 entity = BeanUtil.copyProperties(dto, Demo1.class);
        return Result.ok(demo1Service.save(entity));
    }

    @ApiOperation(value = "DEMO_1更新")
    @PutMapping
    public Result<Boolean> update(@RequestBody Demo1DTO dto) {
        Demo1 entity = BeanUtil.copyProperties(dto, Demo1.class);
        return Result.ok(demo1Service.updateById(entity));
    }

    @ApiOperation(value = "DEMO_1删除")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.ok(demo1Service.removeById(id));
    }

    @ApiOperation(value = "DEMO_1详情")
    @GetMapping("/{id}")
    public Result<Demo1VO> detail(@PathVariable("id") Long id) {
        return Result.ok(BeanUtil.copyProperties(demo1Service.getById(id), Demo1VO.class));
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchDelete")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids){
        return Result.ok(demo1Service.removeByIds(ids));
    }


}

